package org.brain.bean;

import java.util.ArrayList;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

import org.brain.dbo.DBOperate;
import org.brain.pojos.Document;
import org.brain.pojos.Messagetable;
import org.brain.pojos.User;
import org.brain.tool.ToolBean;

@ManagedBean(name="messagebean")
@SessionScoped
public class MessageBean {
	private String username;
	private int messageType=0;
	private int messageid;
	private List<Messagetable> mesList = new ArrayList();
	Messagetable mestable= new Messagetable();
	private int list1;
	private int list2;
	private int list3;
	private int list4;
	private int list5;
	
	public void searchMessage1(){
		User user = ToolBean.getUserSession();
		username=user.getUsername();
		mesList.clear();
		mesList = DBOperate.search("Messagetable where messagetype=1 and messageuser='"+username+"'");
		list1=mesList.size();
	}
	public void searchMessage2(){
		User user = ToolBean.getUserSession();
		username=user.getUsername();
		mesList.clear();
		mesList = DBOperate.search("Messagetable where messagetype=2 and messageuser='"+username+"'");
		list2=mesList.size();
	}
	public void searchMessage3(){
		User user = ToolBean.getUserSession();
		username=user.getUsername();
		mesList.clear();
		mesList = DBOperate.search("Messagetable where messagetype=3 and messageuser='"+username+"'");
		list3=mesList.size();
	}
	public void searchMessage4(){
		User user = ToolBean.getUserSession();
		username=user.getUsername();
		mesList.clear();
		mesList = DBOperate.search("Messagetable where messagetype=4 and messageuser='"+username+"'");
		list4=mesList.size();
	}
	public void searchMessage5(){
		User user = ToolBean.getUserSession();
		username=user.getUsername();
		mesList.clear();
		mesList = DBOperate.search("Messagetable where messagetype=5 and messageuser='"+username+"'");
		list5=mesList.size();
	}
	public void searchOne(){
	    messageid =Integer.parseInt(ToolBean.getRequestParameter("messageid"));
		mesList.clear();
		mesList = DBOperate.search("Messagetable where messageid="+messageid+"");
		mestable=mesList.get(0);
	}
	
	
	public void searchMessage(){
		mesList.clear();
		if("" != ToolBean.getRequestParameter("messageType") && null != ToolBean.getRequestParameter("messageType"))
		{
			 messageType = Integer.parseInt(ToolBean.getRequestParameter("messageType"));
		}
		
		switch(messageType)
		{
			case 1:
				searchMessage1();break;
			case 2:
				searchMessage2();break;
			case 3:
				searchMessage3();break;
			case 4:
				searchMessage4();break;
			case 5:
				searchMessage5();break;
		}
	}
	public boolean delMessage()
	{
		boolean flag = false;
		System.out.println(messageid);
		System.out.println("delete start....");
		Messagetable mest = DBOperate.getMessageobj(messageid);
		flag = DBOperate.del(mest);
		System.out.println("delete end...");
		
		return flag;
	}
	public boolean chaMessage()
	{
		boolean flag = false;
		System.out.println(messageid);
		System.out.println("change start....");
		Messagetable mest = DBOperate.getMessageobj(messageid);
		if(mest.getMessagetype() == 4)
		{
			String u=mest.getMessageuser();
			String uf=mest.getMessagefrom();
			mest.setMessagefrom(u);
			mest.setMessageuser(uf);
		}
        mest.setMessagetype(2);
		System.out.println("change end...");
		DBOperate.update(mest);
		return flag;
	}
	public boolean movMessage(){
		boolean flag = false;
		System.out.println(messageid);
		System.out.println("move start....");
		Messagetable mest = DBOperate.getMessageobj(messageid);
        mest.setMessagetype(5);
		System.out.println("move end...");
		DBOperate.update(mest);
		return flag;
		
	}
	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public List<Messagetable> getMesList() {
		searchMessage();
		return mesList;
	}

	public void setMesList(List<Messagetable> mesList) {
		this.mesList = mesList;
	}
	public int getList1() {
		searchMessage1();
		return list1;
	}
	public void setList1(int list1) {
		this.list1 = list1;
	}
	public int getList2() {
		searchMessage2();
		return list2;
	}
	public void setList2(int list2) {
		this.list2 = list2;
	}
	public int getList3() {
		searchMessage3();
		return list3;
	}
	public void setList3(int list3) {
		this.list3 = list3;
	}
	public int getList4() {
		searchMessage4();
		return list4;
	}
	public void setList4(int list4) {
		this.list4 = list4;
	}
	public int getList5() {
		searchMessage5();
		return list5;
	}
	public void setList5(int list5) {
		this.list5 = list5;
	}
	public int getMessageType() {
		return messageType;
	}
	public void setMessageType(int messageType) {
		this.messageType = messageType;
	}
	public void setMessageid(int messageid) {
		this.messageid = messageid;
	}
	public int getMessageid() {
		return messageid;
	}
	public Messagetable getMestable() {
		searchOne();
		return mestable;
	}
	public void setMestable(Messagetable mestable) {
		this.mestable = mestable;
	}

}
